<!-- 公共文件 start -->
@extends('admin.public.base')
<!-- 公共文件 end -->
@section('content')
    <style>
        .layui-table td, .layui-table th {
            min-width: 70px;
        }
        .layui-card-body {
            transition: all 0.3s ease;
        }
        .layui-card-body.expand {
            height: 200px !important;
            overflow-y: scroll !important;
        }
    </style>
    <div class="x-nav">
    <span class="layui-breadcrumb mbx_fl_10">
        <a href="">首页</a>
        <a href="">订单管理</a>
        <a><cite>出货订单列表</cite></a>
    </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
           onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
        </a>
    </div>
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body" id="searchCardBody">
                        <form class="layui-form layui-col-space5">
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" name="order_sn" placeholder="订单号" autocomplete="off"
                                       value="{{ Request()->get('order_sn') }}" class="layui-input">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" name="stock_order_sn" placeholder="出库单号" autocomplete="off"
                                       value="{{ Request()->get('stock_order_sn') }}" class="layui-input">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" name="src_order_no" placeholder="旺店通订单号" autocomplete="off"
                                       value="{{ Request()->get('src_order_no') }}" class="layui-input">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" name="tracking_number" placeholder="快递单号" autocomplete="off"
                                       value="{{ Request()->get('tracking_number') }}" class="layui-input">
                            </div>
                            <div class="layui-inline">
                                <select name="store_id" lay-filter="storeSelect" lay-search="">
                                    <option value="">选择商户</option>
                                    @foreach( $storeList as $kk => $item)
                                        <option value="{{$item['id']}}"
                                                @if(Request()->get('store_id') == $item['id']) selected @endif>{{$item['store_name']}}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" name="order_mobile" placeholder="联系电话" autocomplete="off"
                                       value="{{ Request()->get('order_mobile') }}" class="layui-input">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <input class="layui-input" autocomplete="off" placeholder="开始日" name="start_time"
                                       id="start" value="{{request()->input('start_time')}}">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <input class="layui-input" autocomplete="off" placeholder="截止日" name="end_time" id="end"
                                       value="{{request()->input('end_time')}}">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" lay-submit="" lay-filter="sreach">
                                    <i class="layui-icon">&#xe615;</i>
                                </button>
                                <input type="hidden" id="export_excel" name="export_excel" value="0">
                                <button id="export_excel_btn" class="layui-btn" lay-submit="" lay-filter="sreach"><i
                                        class="layui-icon">&#xe65b;</i>导出Excel
                                </button>
                            </div>
                        </form>
                    </div>

                    <div class="layui-card-body ">
                        <table class="layui-table layui-form" style="width: 100%">
                            <thead>
                            <tr>
                                <th>订单编号</th>
                                <th>出库单号</th>
                                <th>旺店通订单号</th>
                                <th>快递单号</th>
                                <th>代理商</th>
                                <th>基本折扣</th>
                                <th>商品总金额</th>
{{--                                <th>商品实付</th>--}}
                                <th>运费</th>
                                <th>订单总金额</th>
                                <th>发货快递</th>
                                <th>收货信息</th>
                                <th>审核状态</th>
                                <th>发货状态</th>
                                <th>下单时间</th>
                                <th>操作</th>
                            </thead>
                            <tbody>
                            @foreach($data as $item)
                                <tr>
                                    <td>
                                        {{$item['order_sn']}}
                                    </td>
                                    <td>
                                        {{$item['stock_order_sn']}}
                                    </td>
                                    <td>
                                        {{$item['src_order_no']}}
                                    </td>
                                    <td>
                                        {{$item['tracking_number']}}
                                    </td>
                                    <td>
                                        {{$item['store_name']}}
                                    </td>
                                    <td>
                                        {{$item['discount_rate']}}
                                    </td>
                                    <td>
                                        {{$item['goods_amount']}}
                                    </td>

{{--                                    <td>--}}
{{--                                        {{$item['goods_pay_amount']}}--}}
{{--                                    </td>--}}
                                    <td>
                                        {{$item['freight']}}
                                    </td>
                                    <td>
                                        {{$item['order_amount']}}
                                    </td>
                                    <td>
                                        {{$item['express_company'] ?? ''}}
                                    </td>
                                    <td>
                                        姓名：{{$item['order_name']}}<br>
                                        电话：{{$item['order_mobile']}}<br>
                                        省市：{{$item['order_province'].'-'.$item['order_city'].'-'.$item['order_area']}}
                                        <br>
                                        地址：{{$item['order_address']}}
                                    </td>
                                    <td>
                                        <b style="color:{{$item['order_status_color']}}">{{$item['order_status_name']}}</b>
                                    </td>
                                    <td>
                                        <b style="color:{{$item['send_status_color']}}">{{$item['send_status_name']}}</b>
                                    </td>
                                    <td>
                                        {{$item['created_at']}}
                                    </td>
                                    <td class="td-manage" style="min-width: 180px">
                                        <button class="layui-btn layui-btn layui-btn-xs"
                                                onclick="xadmin.open('查看','/admin/order/orderEdit?id={{$item['id']}}')">
                                            <i class="layui-icon">&#xe642;</i>查看
                                        </button>
                                        <a href="/admin/order/orderPdf?id={{$item['id']}}">
                                            <button class="layui-btn layui-btn-primary">
                                                <i class="icon iconfont">&#xe6c9;</i> 打印
                                            </button>
                                        </a>
                                        @if($item['order_status'] == 3)
                                            <button class="layui-btn layui-btn-primary"
                                                    onclick="delInfo(this, {{$item['id']}})">删除
                                            </button>
                                        @endif
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                    <div class="layui-card-body ">
                        <div class="page">
                            {{ $data->links('admin/public/page') }}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        // 确保在页面加载完成后执行
        $(document).ready(function() {
            var $searchCardBody = $('#searchCardBody');

            // 监听Layui生成的select的实际可点击输入框的点击事件
            // 这个输入框是负责打开下拉框的
            $(document).on('click', 'select[name="store_id"] + .layui-form-select .layui-select-title input.layui-input', function() {
                console.log('Layui select input clicked, adding height.'); // 调试日志
                $searchCardBody.attr('style', 'height: 200px');
            });

            // 监听点击其他地方时收起
            $(document).on('click', function(e) {
                // 检查点击目标是否不在 Layui 生成的 select 区域内，并且不在搜索卡片区域内
                if (!$(e.target).closest('select[name="store_id"] + .layui-form-select').length &&
                    !$(e.target).closest($searchCardBody).length) {
                    console.log('Clicked outside, removing height.'); // 调试日志
                    $searchCardBody.removeAttr('style');
                }
            });
        });

        layui.use(['laydate', 'form'], function () {
            var laydate = layui.laydate;
            var form = layui.form;

            //执行一个laydate实例
            laydate.render({
                elem: '#start', //指定元素
                type: 'datetime'
            });

            //执行一个laydate实例
            laydate.render({
                elem: '#end', //指定元素
                type: 'datetime'
            });

            // 监听select的选择事件 (Layui表单事件)，选择后移除样式
            form.on('select(storeSelect)', function(data){
                console.log('Layui select value changed, removing height.'); // 调试日志
                $('#searchCardBody').removeAttr('style');
            });

            // 重新渲染表单
            form.render();
        });

        function delInfo(obj, id) {
            layer.confirm('确认要删除吗？', function (index) {
                //发异步删除数据
                $.ajax({
                    type: 'POST',
                    url: '/admin/order/orderDel',
                    data: {
                        id: id
                    },
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            $(obj).parents("tr").remove();
                            layer.msg('已删除', {icon: 1, time: 2000});
                        } else {
                            layer.alert(data.msg, {icon: 7, time: 2000, closeBtn: false, btn: []});
                        }
                    },
                    error: function (xhr, type) {
                        alert('Ajax error!')
                    }
                });
            });
        }

        // 删除
        function goods_del(obj, id) {
            layer.confirm('确认要删除吗？', function (index) {
                //发异步删除数据
                $.ajax({
                    type: 'POST',
                    url: '/admin/goods/goodsDel',
                    data: {
                        id: id
                    },
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            $(obj).parents("tr").remove();
                            layer.msg('已删除', {icon: 1, time: 2000});
                        } else {
                            layer.alert(data.msg, {icon: 7, time: 2000, closeBtn: false, btn: []});
                        }
                    },
                    error: function (xhr, type) {
                        alert('Ajax error!')
                    }
                });
            });
        }

        // 分类展开收起的分类的逻辑
        $(function () {
            $("tbody.x-cate tr[fid!='0']").hide();
            // 栏目多级显示效果
            $('.x-show').click(function () {
                if ($(this).attr('status') == 'true') {
                    $(this).html('&#xe625;');
                    $(this).attr('status', 'false');
                    cateId = $(this).parents('tr').attr('cate-id');
                    $("tbody tr[fid=" + cateId + "]").show();
                } else {
                    cateIds = [];
                    $(this).html('&#xe623;');
                    $(this).attr('status', 'true');
                    cateId = $(this).parents('tr').attr('cate-id');
                    getCateId(cateId);
                    for (var i in cateIds) {
                        $("tbody tr[cate-id=" + cateIds[i] + "]").hide().find('.x-show').html('&#xe623;').attr('status', 'true');
                    }
                }
            })
        })

        var cateIds = [];

        function getCateId(cateId) {
            $("tbody tr[fid=" + cateId + "]").each(function (index, el) {
                id = $(el).attr('cate-id');
                cateIds.push(id);
                getCateId(id);
            });
        }

        $("#export_excel_btn").on('click', function () {
            layer.load(2, {
                time: 10000
            });
            $("#export_excel").val(1)
        });
        $("#search").on('click', function () {
            layer.load(2, {
                time: 10000
            });
            $("#export_excel").val(0);
        });
    </script>
@endsection
